package io.hepu.robotize.util;


import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Stream;

public final class Limits {

    private Limits() { throw new AssertionError("No Limits instances for you!"); }

    public static <T> T set(T object, T defaultValue) {
        return isEmpty(object) ? defaultValue : object;
    }

    public static boolean isNull(Object object) {
        return (object == null);
    }

    public static boolean nonNull(Object object) {
        return (object != null);
    }

    public static boolean isEmpty(Object object) {
        if (object == null) {
            return true;
        }

        if (object instanceof Optional) {
            return !((Optional<?>) object).isPresent();
        }

        if (object instanceof CharSequence) {
            return ((CharSequence) object).length() == 0;
        }

        if (object instanceof Collection) {
            return ((Collection<?>) object).isEmpty();
        }

        if (object instanceof Map) {
            return ((Map<?, ?>) object).isEmpty();
        }

        if (object.getClass().isArray()) {
            return Array.getLength(object) == 0;
        }

        if (object instanceof Iterator) {
            return !((Iterator<?>) object).hasNext();
        }

        if (object instanceof Enumeration) {
            return !((Enumeration<?>) object).hasMoreElements();
        }

        return false;
    }

    public static boolean nonEmpty(Object object) {
        return !isEmpty(object);
    }

    public static boolean isValid(Object object) {
        return !isEmpty(object);
    }

    public static boolean isPositive(Object object) {
        return nonNull(object) && new BigDecimal(object.toString()).compareTo(BigDecimal.ZERO) > 0;
    }

    public static boolean equals(Object left, Object right) {
        return Objects.equals(String.valueOf(left), String.valueOf(right));
    }
}
